package com.eos.control.action;

import com.eos.business.service.SysUserBusinessService;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.SysUser;
import com.hf.utils.MD5Util;

public class SavePasswordAction extends AbstractBaseAction {
	private static final long serialVersionUID = -7814990617060056296L;
	private String oldPassword;
	private String newPassword;
	private String rnewPassword;
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getRnewPassword() {
		return rnewPassword;
	}

	public void setRnewPassword(String rnewPassword) {
		this.rnewPassword = rnewPassword;
	}

	@Override
	public String execute() {
		try{
			SysUser user = (SysUser) getSession().getAttribute("SysUser");
			if(!new MD5Util().encrypt(oldPassword).equals(user.getPassWord())){
				statusCode = "300";
				message = "旧密码输入错误, 请重新输入!";
			}else{
				if(!newPassword.equals(rnewPassword)){
					statusCode = "300";
					message = "两次密码输入的不匹配, 请重新输入!";
				}else{
					if(oldPassword.equals(newPassword)){
						statusCode = "300";
						message = "新旧密码一样, 请重新输入!";
					}else{
						user.setPassWord(newPassword);
						user.setModifyTime(ObjectUtils.formatDate(null, null));
						SysUserBusinessService sbs = new SysUserBusinessService();
						String flag = sbs.update(user);
						if("SUCCESS".equals(flag)){
							statusCode = "200";
							message = "密码修改成功!";
						}else{
							statusCode = "300";
							message = "系统繁忙, 修改密码失败!";
						}
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			statusCode = "300";
			message = "系统异常, 修改密码失败!";
		}
		return SUCCESS;
	}
}
